package org.ecpkn.PAGridSim.FastOptimizationWithoutSimulation.LargestTaskFirst;

import java.util.Random;

import org.ecpkn.PAGridSim.core.configuration.Configuration;
import org.ecpkn.PAGridSim.core.tasksCreation.PeriodTaskCreator;

public class DynamicTaskNumberRandomTaskCreator extends PeriodTaskCreator {
	private static final class Const {

		// long seed = 11L*13*15*17*19+21;
		// long seed = 12L*33*52*75*92+3;
		// long seed = 547143986;
		// long SEED = 4L * 38 * 62 * 17 * 28 + 33;
		private static final long SEED = 4L * 65 * 32 * 73 * 18 + 03;

		// task's length is measured by time:a CPU of standard rating takes that
		// quantity of time to execute a job of this task
		private static final int MAX_TIMELENGTH = 50;

	}

	private static Random random = new Random(Const.SEED);
	public static int taskNumber = 0;

	public void setTaskNumber(int t) {
		taskNumber = t;
	}

	@Override
	protected double defineTaskPeriod(int taskNumber) {
		// random double value is between 0-1
		return Configuration.getInstance().toDecimalFormatDouble(
				taskLength.get(taskNumber) * (1 + random.nextDouble() * 5));
	}

	@Override
	protected double defineTaskLength(int taskNumber) {
		// random double value is between 0-1
		double length = Configuration.getInstance().toDecimalFormatDouble(
				random.nextDouble() * Const.MAX_TIMELENGTH);
		taskLength.add(taskNumber, length);
		return length;
	}

	@Override
	protected double defineTaskArrivingTime(int taskNumber) throws Exception {
		String mode = Configuration.getInstance().getParameterString(
				Configuration.Const.TASKS_ARRIVING_MODE);

		if (Configuration.Const.ARRIVING_MODE_RANDOM.equals(mode)) {
			return Configuration.getInstance().toDecimalFormatDouble(
					random.nextDouble());
		} else if (Configuration.Const.ARRIVING_MODE_START_TOGETHER
				.equals(mode)) {
			return 0;
		} else {
			// TODO:change to enum
			throw new Exception("Tasks arriving mode not yet managed");
		}
	}

	@Override
	protected int defineTaskRepeatNumber(int taskNumber) {
		return random.nextInt(50) + 10;
	}

	@Override
	protected int defineTaskSetSize() {

		return taskNumber;
	}

	@Override
	protected double reDefinePeriod(int TaskNumber) {
		double period = defineTaskPeriod(TaskNumber);
		if (period < MINPERIOD) {
			period = MINPERIOD;
		}
		return period;
	}
}
